package com.furongji.education.mq;

import com.furongji.education.config.RabbitConfiguration;
import com.furongji.education.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class RabbitOrderListener {

    private final OrderService orderService;

    // 监听死信队列，收到超时订单消息后取消订单
    @RabbitListener(queues = RabbitConfiguration.ORDER_DEAD_LETTER_QUEUE)
    public void handleOrderTimeout(String orderId) {
        System.err.println("开始订单超时取消...");

        try {
            orderService.cancelOrder(orderId);
            System.err.println("订单超时取消成功：" + orderId);
        } catch (Exception e) {
            System.err.println("订单超时取消失败：" + orderId);
        }
    }
}
